#!/bin/bash 

NODE1_CONFDIR=/tmp/backup_htmf_conf
PATHENV=/usr/local/certificate-services/environment


echo "Welcome to $0, a tool to upgrade/install secondary nodes for Smart Card 2.0"

runningid=`id -u`
if [ "x${runningid}" != "x0" ] ; then
    echo "must run as root eg: sudo $0"
    exit
fi

if [ -r ${PATHENV} ] ; then
	source ${PATHENV}
else
	echo "environment: ${PATHENV} missing"
	exit
fi


replaceconffile() {
    fromfile=$1
    tofile=$2
    
    if [ -f "${tofile}" ] ; then
	echo "found file in place, moved it"
	echo mv "${tofile}" "${tofile}.bak"
	mv "${tofile}" "${tofile}.bak"
    fi
    
    if [ -f "${fromfile}" ] ; then
	echo cp "${fromfile}" "${tofile}"
	cp "${fromfile}" "${tofile}"
    else
	echo "${fromfile} not found, exiting, did you run 921_copy_htmf_conf.sh on node1?"
	exit
    fi
    
}


echo "you need to put properties in place yourself from node one to /tmp/htmf_conf_bak"
echo "Run <syscheck>/related-availabe/921_copy_htmf_conf.sh on node1"
echo "press enter to continiue"
read a


primecard=n
echo
echo "Will you use PrimeCard as CA Token?"
echo "y/n (default:n) >"
read inputprimecard
if [ "x$inputprimecard" = "xy" -o "x$inputprimecard" = "xY" ] ; then
    primecard=y
fi


secondarynode=n
echo
echo "Is this node a secondary node?"
echo "y/N (default:n) >"
read inputsecondarynode
if [ "x$inputsecondarynode" = "xy" -o "x$inputsecondarynode" = "xY" ] ; then
    secondarynode=y
fi


InstallLogFile="/tmp/smartcard20-setup.$$"
echo "To follow the verbose output of the install in a new screen run: # sudo tail -f $InstallLogFile"


#### copy backed up config

echo -n "Step 1/10 Copying backed up config: "

replaceconffile "${NODE1_CONFDIR}/hardtokenmgmt.properties" "${HTMF_HOME}/hardtokenmgmt.properties"  >> ${InstallLogFile} 2>&1
chown jboss.jboss ${HTMF_HOME}/hardtokenmgmt.properties  >> ${InstallLogFile} 2>&1
chmod 644 ${HTMF_HOME}/hardtokenmgmt.properties  >> ${InstallLogFile} 2>&1

replaceconffile "${NODE1_CONFDIR}/autogenerated_hardtokenmgmt.properties" "${HTMF_HOME}/autogenerated_hardtokenmgmt.properties"  >> ${InstallLogFile} 2>&1
chown jboss.jboss ${HTMF_HOME}/autogenerated_hardtokenmgmt.properties  >> ${InstallLogFile} 2>&1
chmod 644 ${HTMF_HOME}/autogenerated_hardtokenmgmt.properties  >> ${InstallLogFile} 2>&1

replaceconffile "${NODE1_CONFDIR}/global.properties" "${HTMF_HOME}/src/resources/globalsettings/global.properties"  >> ${InstallLogFile} 2>&1
chown jboss.jboss ${HTMF_HOME}/src/resources/globalsettings/global.properties  >> ${InstallLogFile} 2>&1
chmod 644 ${HTMF_HOME}/src/resources/globalsettings/global.properties  >> ${InstallLogFile} 2>&1

replaceconffile "${NODE1_CONFDIR}/jarsigner.jks" "${HTMF_HOME}/jarsigner.jks"  >> ${InstallLogFile} 2>&1
chown jboss.jboss ${HTMF_HOME}/jarsigner.jks  >> ${InstallLogFile} 2>&1
chmod 644 ${HTMF_HOME}/jarsigner.jks  >> ${InstallLogFile} 2>&1
  

replaceconffile "${NODE1_CONFDIR}/ejbca.properties" "${EJBCA_HOME}/conf/ejbca.properties"  >> ${InstallLogFile} 2>&1
chown jboss.jboss "${EJBCA_HOME}/conf/ejbca.properties"  >> ${InstallLogFile} 2>&1
chmod 644 "${EJBCA_HOME}/conf/ejbca.properties"  >> ${InstallLogFile} 2>&1

replaceconffile "${NODE1_CONFDIR}/database.properties" "${EJBCA_HOME}/conf/database.properties"  >> ${InstallLogFile} 2>&1
chown jboss.jboss "${EJBCA_HOME}/conf/database.properties"  >> ${InstallLogFile} 2>&1
chmod 644 "${EJBCA_HOME}/conf/database.properties"  >> ${InstallLogFile} 2>&1

mkdir ${JBOSS_HOME}/server/default/conf/keystore/  >> ${InstallLogFile} 2>&1
replaceconffile "${NODE1_CONFDIR}/keystore.jks" "${JBOSS_HOME}/server/default/conf/keystore/keystore.jks"  >> ${InstallLogFile} 2>&1
chown jboss.jboss "${JBOSS_HOME}/server/default/conf/keystore/keystore.jks"  >> ${InstallLogFile} 2>&1
chmod 644 "${JBOSS_HOME}/server/default/conf/keystore/keystore.jks"  >> ${InstallLogFile} 2>&1

replaceconffile "${NODE1_CONFDIR}/truststore.jks" "${JBOSS_HOME}/server/default/conf/keystore/truststore.jks"  >> ${InstallLogFile} 2>&1
chown jboss.jboss "${JBOSS_HOME}/server/default/conf/keystore/truststore.jks"  >> ${InstallLogFile} 2>&1
chmod 644 "${JBOSS_HOME}/server/default/conf/keystore/truststore.jks"  >> ${InstallLogFile} 2>&1

replaceconffile "${NODE1_CONFDIR}/server.xml" "${JBOSS_HOME}/server/default/deploy/jboss-web.deployer/server.xml"  >> ${InstallLogFile} 2>&1
chown jboss.jboss "${JBOSS_HOME}/server/default/deploy/jboss-web.deployer/server.xml"  >> ${InstallLogFile} 2>&1
chmod 644 "${JBOSS_HOME}/server/default/deploy/jboss-web.deployer/server.xml"  >> ${InstallLogFile} 2>&1

echo "do remember to remove the files in ${NODE1_CONFDIR} when you've tested all works ok"
echo "ok"

#### stop  jboss 
echo -n "Step 2/10 Stopping jboss: "
/etc/init.d/jboss stop >> ${InstallLogFile} 2>&1
checkIfJbossIsRunning=`ps -ef | grep java | grep jboss | grep -v grep | awk '{print $2}'`
if [ "x${checkIfJbossIsRunning}" != "x" ] ; then
    sleep 5 
    checkIfJbossIsRunning=`ps -ef | grep java | grep jboss | grep -v grep | awk '{print $2}'`
    if [ "x${checkIfJbossIsRunning}" != "x" ] ; then
	kill -9 ${checkIfJbossIsRunning} 
    fi
fi	

cp ${MYSQLDRIVER} ${JBOSS_HOME}/server/default/lib
for file in ${DISTFILES_INSTALL}/${EXTRA_APPSRV_JARS}/* ; do
       cp $file ${JBOSS_HOME}/server/default/lib
done



${SMARTCARD20_HOME}/jboss-server-switch-log-level.pl INFO
chown -R jboss.jboss ${JBOSS_HOME}/server/default/lib/*jar
chmod 644 ${JBOSS_HOME}/server/default/lib/*jar


echo "ok"

#### stop/start pcscd
echo -n "Step 3/10 restarting pcscd: "
/etc/init.d/pcscd-usr-local stop >> ${InstallLogFile} 2>&1
checkIfPcscIsRunning=`ps -ef | grep pcsc |  grep -v grep | awk '{print $2}'`
if [ "x${checkIfPcscIsRunning}" != "x" ] ; then
	kill -9 ${checkIfPcscIsRunning}
fi	
/etc/init.d/pcscd-usr-local start >> ${InstallLogFile} 2>&1

echo "ok"



### setting up htmf options

echo -n "Step 4/10 PrimeCard settings: "

### primecard props
if [ "x$primecard" = "xy" ] ;then
    cd ${PRIMECARD_HOME}/
    ./bin/caTokenCard.sh >> ${InstallLogFile} 2>&1
    
#primecard setting in ejbca.properies
# hwtoken -> /usr/local/primecard/caTokenClasses-1.6-3.5/
    
    filename=`./bin/changePIN.sh listkeys | grep 'List of keys on card written to file' | perl -ane 'm/List of keys on card written to file\ (.*)\./, print "$1"'`
    mv $filename ${HTMF_HOME}/active-catoken.txt
    chown jboss.jboss ${HTMF_HOME}/active-catoken.txt
    chmod 644 ${HTMF_HOME}/active-catoken.txt
    
fi
	

echo "ok"



#### ejbca bootstrap
echo -n "Step 5/10 Running ejbca bootstrap (prepairing the CA for setup): "
cd ${EJBCA_HOME}

su -p -s /bin/sh jboss -c "ant bootstrap" >> ${InstallLogFile} 2>&1
if [ $? -eq 0 ] ; then
    echo "ok"
else
    echo
    echo "FAILED: ant bootstrap failed, see file: ${InstallLogFile} for more info"
    exit 1
fi


#### htmf deploy
echo -n "Step 6/10 Running htmf deploy: "
cd ${HTMF_HOME}

su -p -s /bin/sh jboss -c "ant deploy" >> ${InstallLogFile} 2>&1
if [ $? -eq 0 ] ; then
    echo "ok"
else
    echo
    echo "FAILED: htmf deploy failed, see file: ${InstallLogFile} for more info"
    exit 1
fi


#### start jboss

echo -n "Step 7/10 Starting jboss: "
/etc/init.d/jboss start	>> ${InstallLogFile} 2>&1
echo "ok"


if [ "x${secondarynode}" != "xy" ] ; then

  #### htmf:upgrade
  echo -n "Step 8/10 Running htmf:upgrade , Now you will have to answer some questions about this installation: "

  cd $HTMF_HOME

  su -p -s /bin/sh jboss -c "ant htmf:upgrade" 
  if [ $? -eq 0 ] ; then
    echo "ok"
  else
    echo
    echo "FAILED: upgrade failed"
    exit 5
  fi


  #### restart jboss

  echo -n "Step 9/10 Restarting Jboss: "
  /etc/init.d/jboss stop >> ${InstallLogFile} 2>&1
  checkIfJbossIsRunning=`ps -ef | grep java | grep jboss | grep -v grep | awk '{print $2}' `
  if [ "x${checkIfJbossIsRunning}" != "x" ] ; then
    sleep 5
    kill -9 ${checkIfJbossIsRunning}
  fi       

  sleep  10
  /etc/init.d/jboss start >> ${InstallLogFile} 2>&1

  echo "ok"

  #### success
fi


echo -n "Step 10/10 Activating CA:s : "
	
if [ "x$primecard" = "xy" ] ; then
    echo
    echo "activating the CA:s"
    ${INSTALL_INTO}/smartcard20/activate-cas.sh
    echo "to activate CA:s again (after jboss or server restart) run: ${INSTALL_INTO}/smartcard20/activate-cas.sh"
    echo 
fi
echo "ok"

echo 
echo 
echo "$0 finished"
echo 
echo 
exit 0

